package com.xjj.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjj.common.BaseContext;
import com.xjj.common.R;
import com.xjj.entity.OrderDetail;
import com.xjj.entity.Orders;
import com.xjj.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/order")
@Slf4j
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;


    /**
     * 查询列表页接口
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(ordersPage, queryWrapper);
        return R.success(ordersPage);
    }


    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Long id = BaseContext.getCurrentId();
        log.info("已登录用户ID==={}",id);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,id);
        orderService.page(ordersPage,queryWrapper);

        return R.success(ordersPage);
    }

    @PostMapping("/submit")
    public R<String> addOrder(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }
}
